﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;

namespace Actya.Web.Mvc
{
	public class ActyaControllerFactory : DefaultControllerFactory
	{
		private readonly IContainer _container;

		public ActyaControllerFactory(IContainer container)
		{
			_container = container;
		}
		protected override Type GetControllerType(RequestContext requestContext, string controllerName)
		{
			var controllerType = base.GetControllerType(requestContext, controllerName);
			if (controllerType == null)
			{
				object x;
				if (_container.TryResolveNamed(controllerName, typeof(IController), out x))
					controllerType = x.GetType();
			}

			return controllerType;
		}
	}
}